package gui;

import business.ValidationService;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

@Named(value = "usernameValidatorRegistration")
@ManagedBean
@RequestScoped
public class UsernameValidatorRegistration implements Validator {

    @EJB
    private ValidationService validator;

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (value != null) {
            String username = (String) value;

            if (validator.existUsername(username)) {
                throw new ValidatorException(new FacesMessage("Username already exists."));
            }
        }
    }
}